/* 
 * Copyright 2011 kOSMik.
 * This file is part of kOSMik.
 * 
 * kOSMik is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * kOSMik is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with kOSMik.  If not, see <http://www.gnu.org/licenses/>.
 *  
 * kOSMik:
 * Open Street Map - Editor.
 * http://code.google.com/p/swp-dv-ws2010-osm-2/
 */

package de.fuberlin.kosmik.export;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Collection;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;
import de.fuberlin.kosmik.R;
import de.fuberlin.kosmik.data.DataBaseConnector;
import de.fuberlin.kosmik.data.DataBaseConnectorImpl;
import de.fuberlin.kosmik.data.dao.Poi;
import de.fuberlin.kosmik.data.dao.Point;
import de.fuberlin.kosmik.files.FileManager;
import de.fuberlin.kosmik.files.FileManagerImpl;
import de.fuberlin.kosmik.log.Constants;

/**
 * Takes care of asynchronously exporting a track to a GPX file.
 * 
 * @author Simon
 * 
 */
public class GpxExporter extends AsyncTask<Long, Void, Boolean> {
	private static final String GPX_FILE_EXTENSION = ".gpx";

	private Context context;
	private ProgressDialog dialog;

	/**
	 * Constructor. Takes care of file system, export and notifying the user.
	 * 
	 * @param context context of the application.
	 */
	public GpxExporter(Context context) {
		this.context = context;
		dialog = ProgressDialog.show(context, "",
				context.getString(R.string.osm_export_pls_wait), true);
	}

	@Override
	protected Boolean doInBackground(Long... tracks) {
		try {
			long trackId = tracks[0].longValue();
			Log.d(Constants.TAG, "[GPXEXPORTER] START EXPROT OF TRACK WITH ID =" + trackId);

			/**
			 * Setup.
			 */
			FileManager fm = new FileManagerImpl(trackId);
			File f = fm.putFile(Long.toString(trackId) + GPX_FILE_EXTENSION);

			OutputStream transport;
			transport = new FileOutputStream(f);

			XmlWriter xml = new XmlWriter(transport);
			GpxWriter writer = new GpxWriter(xml);
			DataBaseConnector dbc = new DataBaseConnectorImpl(context);

			/**
			 * Actual work.
			 */
			Collection<Point> trackPoints = dbc.getPointsFromTrack(trackId);
			Log.d(Constants.TAG,
					"[GPXEXPORTER] WRITING TRACKPOINTS. COUNT =" + trackPoints.size());
			// all points within this track
			writer.writeTrack(Long.toString(trackId), trackPoints);

			Collection<Poi> wayPoints = dbc.getPoisFromTrack(trackId);
			Log.d(Constants.TAG, "[GPXEXPORTER] NUMBER OF POIS=" + wayPoints.size());
			for (Poi wayPoint : wayPoints) {
				writer.writeWayPoint(wayPoint);
			}

			/**
			 * Cleanup.
			 */
			writer.closeRoot();
			dbc.close();
			return Boolean.valueOf(true);

		} catch (IOException e) {
			return Boolean.valueOf(false);
		}
	}

	/**
	 * If doInBackground failed due to an illegal storage state, notify the
	 * user.
	 */
	@Override
	protected void onPostExecute(Boolean success) {
		dialog.dismiss();
		if (!success.booleanValue()) {
			Toast.makeText(context, context.getString(R.string.err_media_not_ready),
					Toast.LENGTH_LONG).show();
		}

		Log.d(Constants.TAG, "[GPXEXPORTER] FINISHED");
	}

}
